<?php
if (isset($_COOKIE["user_dailv"])) {
    header("Location: index.php");
}
?>
<?php
if (isset($_POST["login"])) {
    require 'mysql.php';
    $user = $_POST['user'];    
    $password = md5(mysql_real_escape_string($_POST['password']));
    $sql = "Select * from LeVanDai_tbluser where `User` = '$user' AND `Password` = '$password'";
    $result = mysqli_query($link, $sql);
    $count = mysqli_num_rows($result);
    if ($count > 0) {
        $expire = time() + 60 * 60 * 24 * 30;
        setcookie("user_dailv", "$user", $expire);
        $row = mysqli_fetch_array($result);
        $email = $row["Email"];
        setcookie("email_dailv", "$email", $expire);
        header("Location: index.php");
    } else {
        echo "Username or Password incorrect";
    }
}
if (isset($_POST["reg"])) {
    header("Location: register.php");
}
?>
<html>
    <body style="width: 800px; margin: 20px auto auto auto">
        <form action="" method="post">
            Username: <input type="text" name="user">
            <br>
            Password: <input type="password" name="password">
            <br>
            <input type="submit" name="login" value="Dang nhap" />
            <input type="submit" name="reg" value="Dang ky" />            
        </form>

    </body>
</html>
